package com.hm.cms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.hm.cms.mongo.domain.CmsSeoOptimize;
import com.hm.cms.mongo.domain.QCmsSeoOptimize;
import com.hm.cms.mongo.vo.CmsSeoOptimizeVo;
import com.hm.cms.share.repository.mongo.CmsSeoOptimizeRepository;
import com.hm.cms.share.service.CmsSeoOptimizeService;
import com.hm.common.common.BeanUtil;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.model.PageInfo;

/**
 * @author shishun.wang
 * @date 下午2:25:26 2016年8月6日
 * @version 1.0
 * @describe
 */
@Service
public class CmsSeoOptimizeServiceImpl implements CmsSeoOptimizeService {

	@Autowired
	private CmsSeoOptimizeRepository cmsSeoOptimizeRepository;

	@Override
	public CmsSeoOptimizeVo getCmsSeoOptimize(String id) {
		CmsSeoOptimize seoOptimize = cmsSeoOptimizeRepository.findOne(id);
		if (null == seoOptimize) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}

		return (CmsSeoOptimizeVo) BeanUtil.copyProperties(new CmsSeoOptimizeVo(), seoOptimize);
	}

	@Override
	public String addCmsSeoOptimize(CmsSeoOptimizeVo optimizeVo, String operationUser) {
		CmsSeoOptimize seoOptimize = new CmsSeoOptimize();
		{
			seoOptimize = (CmsSeoOptimize) BeanUtil.copyProperties(seoOptimize, optimizeVo);
			seoOptimize.setCreateUser(operationUser);
			seoOptimize.setId(null);
		}

		return cmsSeoOptimizeRepository.save(seoOptimize).getId();
	}

	@Override
	public String updateCmsSeoOptimize(CmsSeoOptimizeVo optimizeVo, String operationUser) {
		CmsSeoOptimize seoOptimize = cmsSeoOptimizeRepository.findOne(optimizeVo.getId());
		if (null == seoOptimize) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}
		seoOptimize = (CmsSeoOptimize) BeanUtil.copyProperties(seoOptimize, optimizeVo);

		return cmsSeoOptimizeRepository.save(seoOptimize).getId();
	}

	@Override
	public String deleteCmsSeoOptimize(CmsSeoOptimizeVo optimizeVo, String operationUser) {
		cmsSeoOptimizeRepository.delete(optimizeVo.getId());
		return optimizeVo.getId();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public PageInfo<CmsSeoOptimizeVo> queryCmsSeoOptimize(Integer pageNumber, Integer pageSize) {
		PageInfo<CmsSeoOptimizeVo> pageInfo = new PageInfo<CmsSeoOptimizeVo>(pageNumber, pageSize);

		Pageable pageable = new QPageRequest(pageInfo.getPageNumber(), pageInfo.getPageSize(),
				QCmsSeoOptimize.cmsSeoOptimize.createTime.desc());
		Page page = cmsSeoOptimizeRepository.findAll(pageable);

		List<CmsSeoOptimizeVo> vos = new ArrayList<CmsSeoOptimizeVo>();
		page.getContent().forEach(baseInfo -> {
			vos.add((CmsSeoOptimizeVo) BeanUtil.copyProperties(new CmsSeoOptimizeVo(), baseInfo));
		});

		return pageInfo.execute(page, vos);
	}

}
